package com.ruoyi.comprehensive.service.impl;

import java.util.Date;
import java.util.List;
import java.util.Random;
import java.util.stream.Collectors;

import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.comprehensive.domain.FirePreventionExamine;
import com.ruoyi.comprehensive.domain.PreventionEnclosure;
import com.ruoyi.comprehensive.domain.PreventionExamine;
import com.ruoyi.comprehensive.dto.PreventionDto;
import com.ruoyi.comprehensive.mapper.PreventionEnclosureMapper;
import com.ruoyi.comprehensive.service.ICodeService;
import com.ruoyi.comprehensive.service.IPreventionExamineService;
import com.ruoyi.comprehensive.vo.CodeVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.comprehensive.mapper.PreventionMapper;
import com.ruoyi.comprehensive.domain.Prevention;
import com.ruoyi.comprehensive.service.IPreventionService;

/**
 * 通防填报管理Service业务层处理
 * 
 * @author hcc
 * @date 2023-12-16
 */
@Service
public class PreventionServiceImpl implements IPreventionService 
{
    @Autowired
    private PreventionMapper preventionMapper;
    @Autowired
    private PreventionEnclosureMapper preventionEnclosureMapper;
    @Autowired
    private IPreventionExamineService preventionExamineService;
    @Autowired
    private ICodeService codeService;
    /**
     * 查询通防填报管理
     * 
     * @param id 通防填报管理主键
     * @return 通防填报管理
     */
    @Override
    public Prevention selectPreventionById(String id)
    {
        return preventionMapper.selectPreventionById(id);
    }

    /**
     * 查询通防填报管理列表
     * 
     * @param prevention 通防填报管理
     * @return 通防填报管理
     */
    @Override
    public List<Prevention> selectPreventionList(Prevention prevention)
    {
        return preventionMapper.selectPreventionList(prevention);
    }

    /**
     * 新增通防填报管理
     * 
     * @param prevention 通防填报管理
     * @return 结果
     */
    @Override
    public int insertPrevention(Prevention prevention)
    {
        Prevention prevention1 = new Prevention();
        BeanUtils.copyProperties(prevention,prevention1);
        prevention1.setExamineStatus(0L);
        if (prevention1.getMonthNo() != null){
            prevention1.setReportForm("M");
        }
        if (prevention1.getYearNo() != null){
            prevention1.setReportForm("Y");
        }
        prevention1.setCreateByName(SecurityUtils.getUsername());
        CodeVo codeVo = new CodeVo();
        codeVo.setPrefix("TF");
        Random random = new Random();
        codeVo.setClickCount(random.nextInt(100));
        prevention1.setPreventionCode(codeService.getDustPreventionCode(codeVo));
        prevention1.setCreateBy(SecurityUtils.getUserId().toString());
        prevention1.setCreateAt(new Date());
        prevention1.setUpdateAt(new Date());
        prevention1.setUpdateByName(SecurityUtils.getUsername());
        prevention1.setUpdateBy(SecurityUtils.getUserId().toString());
        return preventionMapper.insertPrevention(prevention1);
    }

    /**
     * 修改通防填报管理
     * 
     * @param prevention 通防填报管理
     * @return 结果
     */
    @Override
    public int updatePrevention(Prevention prevention)
    {
        if (prevention.getExamineStatus() != null && prevention.getExamineStatus() == 1L){
            Prevention prevention1 = preventionMapper.selectPreventionById(prevention.getId());
            if (prevention1 != null){
                PreventionExamine preventionExamine = new PreventionExamine();
                BeanUtils.copyProperties(prevention1,preventionExamine);
                preventionExamine.setPreventionId(prevention1.getId());
                preventionExamine.setExamineStatus(1L);
                preventionExamineService.insertPreventionExamine(preventionExamine);
            }
        }
        return preventionMapper.updatePrevention(prevention);
    }

    /**
     * 批量删除通防填报管理
     * 
     * @param ids 需要删除的通防填报管理主键
     * @return 结果
     */
    @Override
    public int deletePreventionByIds(String[] ids)
    {
        return preventionMapper.deletePreventionByIds(ids);
    }

    /**
     * 删除通防填报管理信息
     * 
     * @param id 通防填报管理主键
     * @return 结果
     */
    @Override
    public int deletePreventionById(String id)
    {
        return preventionMapper.deletePreventionById(id);
    }

    /**
     * 获取通防填报管理列表
     * @param prevention
     * @return
     */
    @Override
    public List<PreventionDto> getPreventionList(Prevention prevention) {
        List<PreventionDto> preventionList = preventionMapper.getPreventionList(prevention);
        return preventionList;
    }

    /**
     * 获取通防填报管理详细信息
     * @param id
     * @return
     */
    @Override
    public PreventionDto getPreventionById(String id) {
        PreventionDto preventionDto = new PreventionDto();
        Prevention prevention = preventionMapper.selectPreventionById(id);
        BeanUtils.copyProperties(prevention,preventionDto);
        PreventionEnclosure preventionEnclosure = new PreventionEnclosure();
        preventionEnclosure.setPreventionId(prevention.getId());
        preventionEnclosure.setEnclosureType("5");
        List<PreventionEnclosure> preventionEnclosures = preventionEnclosureMapper.selectPreventionEnclosureList(preventionEnclosure);
        preventionDto.setEnclosures(preventionEnclosures);
        return preventionDto;
    }
}
